package com.wondersgroup.fjzlpt.org.service.impl.gzt;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wondersgroup.fjzlpt.org.dao.inter.hzgl.FpfwDao;
import com.wondersgroup.fjzlpt.org.dao.inter.hzgl.YybDao;
import com.wondersgroup.fjzlpt.org.dao.inter.hzgl.ZzbDao;
import com.wondersgroup.fjzlpt.org.dao.inter.jtqy.QyxxDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.jtqy.ZjsqDao;
import com.wondersgroup.fjzlpt.org.dao.inter.tdgl.TdjbxxDao;
import com.wondersgroup.fjzlpt.org.service.inter.gzt.GztService;

@Service
public class GztServiceImpl implements GztService{

	@Autowired
	private QyxxDAO qyxxDAO;
	@Autowired 
	private TdjbxxDao tdjbxxDao;
	
	@Autowired
	private ZjsqDao zjsqDao;
	@Autowired 
	private ZzbDao zzbDao;
	@Autowired
	private YybDao yybDao;
	@Autowired
	private FpfwDao fpfwDao;
	
	
	
	/**
	 * 查询当前用户所有正常的团队信息
	 * @return
	 */
	public JSONArray queryTdxxListByNowUser(Map<String, String> map){
		return tdjbxxDao.queryTdxxByUserInfo(map);
	}
	
	/**
	 * 签约信息数量的统计查询
	 * @param map
	 * @return
	 */
	@Override
	public JSONObject queryCount(Map<String, String> map) {
		/* 签约信息概览 */
		map.put("QYLX", "01");
		JSONObject yqyNumbersObj = qyxxDAO.queryQyNumbers(map);
		JSONObject zzNumberObj = zzbDao.queryZzNumbers(map);
		JSONObject yyNumberObj = yybDao.queryYyNumbers(map);
		
		JSONObject result = new JSONObject();
		result.put("YQYRC", formatNumber(yqyNumbersObj.getString("YQYRC")));//已签约人次
		result.put("YXQYRC", formatNumber(yqyNumbersObj.getString("YXQYRC"))); //有效签约人次
		result.put("ZZRC", formatNumber(zzNumberObj.getString("ZZRC"))); //转诊人次
		result.put("YYRC", formatNumber(yyNumberObj.getString("YYRC"))); //预约人次
		
		int zzLyrc = formatNumber(zzNumberObj.getString("LYRC")); //转诊履约人次
		int yyLyrc = formatNumber(yyNumberObj.getString("LYRC")); //预约履约人次
		result.put("LYRC", zzLyrc + yyLyrc);
		
		
		/*近期工作任务*/
		map.remove("QYSJ_start");
		map.remove("QYSJ_end");
		//预签约
		map.put("QYLX", "00");
		result.put("wqyNum", qyxxDAO.queryCountOnView(map));
		
		//到期提醒
		this.formatTqsj(map);
		map.put("QYLX","01");
		map.put("SFXY","0");
		result.put("dqtxNum", qyxxDAO.queryCountOnView(map));
		
		//转介审核
		map.put("rows", "1");
		map.put("page", "1");
		map.put("XQYTD", map.get("YLTDID"));
		JSONObject zjshObj = zjsqDao.queryPage(map);
		result.put("zjshNum", formatNumber(zjshObj.getString("total")));
		
		return result;
		
	}
	
	private int formatNumber(String numStr){
		int i = 0;
		if(!"".equals(numStr)){
			i = Integer.parseInt(numStr);
		}
		return i;
	}

	/**
	 * 分页查询"已签约"居民数据
	 * @param map
	 * @return
	 */
	@Override
	public JSONObject queryYqyjmDataByPage(Map<String, String> map) {
		map.put("QYLX", "01");
		//return qyxxDAO.queryPageForGzt(map);
		return qyxxDAO.queryPageOnView(map);
	}

	/**
	 * 分页查询"已预约"居民数据
	 * @param map
	 * @return
	 */
	@Override
	public JSONObject queryYyyjmDataByPage(Map<String, String> map) {
		return yybDao.queryPage(map);
	}

	/**
	 * 分页查询"已转诊"居民数据（转介成功的居民）
	 * @param map
	 * @return
	 */
	@Override
	public JSONObject queryYzzjmDataByPage(Map<String, String> map) {
		return zzbDao.queryPage(map);
	}

	/**
	 * 分页查询"已履约"居民数据
	 * @param map
	 * @return
	 */
	@Override
	public JSONObject queryYlyjmDataByPage(Map<String, String> map) {
		map.put("SFLY", "1");
		JSONObject yyObj = yybDao.queryPage(map);
		JSONObject zzObj = zzbDao.queryPage(map);
		
		int yyTotal = yyObj.getIntValue("total");
		int zzTotal = zzObj.getIntValue("total");
		
		JSONArray resultData = new JSONArray();
		resultData.addAll(yyObj.getJSONArray("rows"));
		resultData.addAll(zzObj.getJSONArray("rows"));
		
		JSONObject result = new JSONObject();
		result.put("total", yyTotal+zzTotal);
		result.put("rows", resultData);
		
		
		return result;
	}
	
	/**
	 * 分页查询"有效签约"居民数据
	 * @param map
	 * @return
	 */
	@Override
	public JSONObject queryYxqyjmDataByPage(Map<String, String> map) {
		map.put("QYLX", "01");
		map.put("YXQY", "1");
		//return qyxxDAO.queryPageForGzt(map);
		return qyxxDAO.queryPageOnView(map);
	}
	
	
	
	

	/**
	 * 分页查询"预签约"居民数据
	 * wqy:未签约
	 * @param map
	 * @return
	 */
	@Override
	public JSONObject queryWqyjmDataByPage(Map<String, String> map) {
		map.put("QYLX", "00");
		//return qyxxDAO.queryPageForGzt(map);
		return qyxxDAO.queryPageOnView(map);
	}

	/**
	 * 分页查询"到期提醒"居民的数据
	 * @param map
	 * @return
	 */
	@Override
	public JSONObject queryDqtxjmDataByPage(Map<String, String> map) {
		this.formatTqsj(map);
		map.put("QYLX","01");
		return qyxxDAO.queryPageOnView(map);
	}

	/**
	 * 格式化提前时间
	 * @param map
	 */
	private void formatTqsj(Map<String, String> map){
		map.put("TQTS","30"); //提前天数  ： 默认提前30天

		Date date = new Date();
		String startTime =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
		map.put("JSSJ_start", startTime);

		//提醒提前时间设定，“天”为单位
		Calendar c = new GregorianCalendar();
		c.setTime(date);
		c.add(Calendar.DATE,Integer.parseInt(map.get("TQTS")));//提前天数
		date=c.getTime();
		String endTime =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
		map.put("JSSJ_end", endTime);
	}
	
}
